﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using ERP_RORZE.Tool.Forms;
using ERP_RORZE_TOOL.BizLogic;
using ERP_RORZE_TOOL.Common;
using ERP_RORZE_TOOL.Common.ValueObject;

namespace ERP_RORZE.Tool.UserControls
{
	public partial class ucQTool : DevExpress.XtraEditors.XtraUserControl
	{
		#region Handle Methods

		public ucQTool()
		{
			InitializeComponent();
			BindDatatoGrid();
		}

		private void btAddNew_Click_1(object sender, EventArgs e)
		{
			AddNew();
		}

		private void btEdit_Click_1(object sender, EventArgs e)
		{
			EditRow();
		}

		private void btDelete_Click_1(object sender, EventArgs e)
		{
			DeleteRows();
		}


		private void gridView1_CustomDrawEmptyForeground_1(object sender, DevExpress.XtraGrid.Views.Base.CustomDrawEventArgs e)
		{
			if(gridView1.RowCount != 0)
				return;

			StringFormat drawFormat = new StringFormat();

			drawFormat.Alignment = drawFormat.LineAlignment = StringAlignment.Center;

			e.Graphics.DrawString("No items exist in this view", e.Appearance.Font, SystemBrushes.ControlDark, new RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height), drawFormat);

		}

		private void gridView1_DoubleClick_1(object sender, EventArgs e)
		{
			var gridView = (GridView)sender;
			var point = gridView.GridControl.PointToClient(Control.MousePosition);

			DoDoubleClick(gridView, point);
		}


		private void addNewToolStripMenuItem_Click(object sender, EventArgs e)
		{
			AddNew();
		}

		private void editToolStripMenuItem_Click(object sender, EventArgs e)
		{
			EditRow();
		}

		private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
		{
			DeleteRows();
		}
		#endregion

		public void BindDatatoGrid()
		{
			QToolHelper helper = new QToolHelper(new QTool());
			DataTable dt = helper.GetAllLeftJoin();

			gridControl1.DataSource = dt;

		}

		private void DoDoubleClick(GridView v, Point p)
		{
			var info = v.CalcHitInfo(p);
			if(info.InRow || info.InRowCell)
			{
				var row = new DataRow[gridView1.SelectedRowsCount];
				row[0] = gridView1.GetDataRow(gridView1.GetSelectedRows()[0]);
				string toolId = row[0][ToolConstants.Entities.QTool.FieldName.QToolID].ToString();
				string makerId = row[0][ToolConstants.Entities.QTool.FieldName.MakerID].ToString();
				string rriName = row[0][ToolConstants.Entities.QTool.FieldName.RRIName].ToString();
				string makerToolName = row[0][ToolConstants.Entities.QTool.FieldName.MakerName].ToString();
				string price = row[0][ToolConstants.Entities.QTool.FieldName.Price].ToString();
				string safeQty = row[0][ToolConstants.Entities.QTool.FieldName.SafeQty].ToString();
				string frequencyId = row[0][ToolConstants.Entities.Frequency.FieldName.FrequencyID].ToString();
				string toolGroupId = row[0][ToolConstants.Entities.ToolGroup.FieldName.ToolGroupID].ToString();

				frm_QToolEdit fr = new frm_QToolEdit(this);
				fr.QToolId = toolId;
				fr.MakerId = makerId;
				fr.RRIToolName = rriName;
				fr.MakerToolName = makerToolName;
				fr.Price = int.Parse(price);
				fr.SafeQty = int.Parse(safeQty);
				fr.FrequencyId = frequencyId;
				fr.ToolGroupId = toolGroupId;

				fr.StartPosition = FormStartPosition.Manual;
				fr.Location = new Point(250, 160);
				fr.ShowDialog();
			}
		}

		private void DeleteRows()
		{
			if(gridView1.SelectedRowsCount >0)
			{
				var row = new DataRow[gridView1.SelectedRowsCount];
				var toolId = new string[gridView1.SelectedRowsCount];

				for(int i=0; i< gridView1.SelectedRowsCount; i++)
				{
					row[i]= gridView1.GetDataRow(gridView1.GetSelectedRows()[i]);
					toolId[i] = row[i][ToolConstants.Entities.QTool.FieldName.QToolID].ToString();
				}


				if(MessageBox.Show(ToolConstants.Message.DeleteQuestion, ToolConstants.Form.MessageBox.Warning,
								MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
				{
					QToolHelper helper = new QToolHelper(new QTool());
					for(int i = 0; i < toolId.Length; i++)
					{
						helper.ValueObject.QToolID = toolId[i];
						if(helper.Delete() >0)
							BindDatatoGrid();
						else
						{
							MessageBox.Show(ToolConstants.Message.DeleteFailed, ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
						}
					}


				}
			}
			else
			{
				MessageBox.Show(ToolConstants.Message.TableIsNull, ToolConstants.Form.MessageBox.Message, MessageBoxButtons.OK,
								MessageBoxIcon.Information);
			}
		}

		private void EditRow()
		{
			if(gridView1.SelectedRowsCount >0)
			{
				frm_QToolEdit fr = new frm_QToolEdit(this);
				var row = new DataRow[gridView1.SelectedRowsCount];
				row[0] = gridView1.GetDataRow(gridView1.GetSelectedRows()[0]);
				string toolId = row[0][ToolConstants.Entities.QTool.FieldName.QToolID].ToString();
				string makerId = row[0][ToolConstants.Entities.QTool.FieldName.MakerID].ToString();
				string rriName = row[0][ToolConstants.Entities.QTool.FieldName.RRIName].ToString();
				string makerToolName = row[0][ToolConstants.Entities.QTool.FieldName.MakerName].ToString();
				string price = row[0][ToolConstants.Entities.QTool.FieldName.Price].ToString();
				string safeQty = row[0][ToolConstants.Entities.QTool.FieldName.SafeQty].ToString();
				string frequencyId = row[0][ToolConstants.Entities.Frequency.FieldName.FrequencyID].ToString();
				string toolGroupId = row[0][ToolConstants.Entities.ToolGroup.FieldName.ToolGroupID].ToString();

				fr.QToolId = toolId;
				fr.MakerId = makerId;
				fr.RRIToolName = rriName;
				fr.MakerToolName = makerToolName;
				fr.Price = int.Parse(price);
				fr.SafeQty = int.Parse(safeQty);
				fr.FrequencyId = frequencyId;
				fr.ToolGroupId = toolGroupId;

				fr.StartPosition = FormStartPosition.Manual;
				fr.Location = new Point(250, 200);
				fr.ShowDialog();
			}
			else
			{
				MessageBox.Show(ToolConstants.Message.TableIsNull, ToolConstants.Form.MessageBox.Message, MessageBoxButtons.OK,
								MessageBoxIcon.Information);
			}
		}

		private void AddNew()
		{
			Form fr = new frm_QToolAdd(this);
			fr.StartPosition = FormStartPosition.Manual;
			fr.Location = new Point(250, 200);
			fr.ShowDialog();
		}

	}
}
